package cn.edu.nju.software.action.user;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.UserShow;
import cn.edu.nju.software.service.intf.UserService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ModifyAddressAction extends ActionSupport implements SessionAware {

	private String name;
	private String area;
	private String address;
	private String zipCode;
	private String phone;
	private UserService userService;
	private Map<String, Object> session;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public String input() throws Exception {
		String userAddress = ((UserShow) session.get(Constants.SESSION_USER)).getAddress();
		if (userAddress != null) {
			String[] splitArray = userAddress.split(Constants.ADDRESS_SEPARATOR);
			if (splitArray.length == 5) {
				name = splitArray[0];
				area = splitArray[1];
				address = splitArray[2];
				zipCode = splitArray[3];
				phone = splitArray[4];
			}
		}
		return INPUT;
	}

	@Override
	public String execute() throws Exception {
		UserShow userOld = (UserShow) session.get(Constants.SESSION_USER);
		String s = Constants.ADDRESS_SEPARATOR;
		userOld.setAddress(name + s + area + s + address + s + zipCode + s + phone);
		userService.updateUser(userOld.getUser());
		return SUCCESS;
	}

}
